package han.chensing.latos.maker.windows.widget;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Widget;

import han.chensing.latos.common.level.content.timeLine.motion.interpolator.Bezier2Interpolator;
import han.chensing.latos.common.level.util.MathUtil;
import han.chensing.latos.maker.GameMain;
import space.earlygrey.shapedrawer.ShapeDrawer;

public class BezierEditorWidget extends Widget {

    private float x1,y1,x2,y2;
    private int movingObject;
    private boolean dirty=true;
    private float[] map;
    private final int dragSide=15;
    private float timePercent;

    Color backgoundColor=new Color(0x1f1e33ff);

    BezierEditorWidgetUpdateInterface bezierEditorWidgetUpdateInterface;

    public BezierEditorWidget(BezierEditorWidgetUpdateInterface bezierEditorWidgetUpdateInterface){
        this.bezierEditorWidgetUpdateInterface=bezierEditorWidgetUpdateInterface;
        addListener(new InputListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                float width = getWidth();
                float height = getHeight();
                float d1X = (x1 * width - dragSide / 2f);
                float d1Y = (y1 * height - dragSide / 2f);
                float d2X = (x2 * width - dragSide / 2f);
                float d2Y = (y2 * height - dragSide / 2f);
                Rectangle d1Rectangle=new Rectangle(d1X,d1Y,dragSide,dragSide);
                Rectangle d2Rectangle=new Rectangle(d2X,d2Y,dragSide,dragSide);
                if (d1Rectangle.contains(x,y))
                    movingObject=1;
                else if (d2Rectangle.contains(x,y))
                    movingObject=2;
                else
                    movingObject=0;
                return true;
            }

            @Override
            public void touchDragged(InputEvent event, float x, float y, int pointer) {
                super.touchDragged(event, x, y, pointer);
                float tx=MathUtil.range(0,(x)/getWidth(),1);
                float ty=MathUtil.range(0,(y)/getHeight(),1);
                if (movingObject==1) {
                    x1 = tx;
                    y1 = ty;
                }else if (movingObject==2){
                    x2=tx;
                    y2=ty;
                }
                dirty=true;
                if (bezierEditorWidgetUpdateInterface!=null)
                    bezierEditorWidgetUpdateInterface.onBezierUpdated(x1,y1,x2,y2);
            }
        });
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        float x = getX();
        float y = getY();
        float width = getWidth();
        float height = getHeight();
        if (dirty)regenMap();
        float xStep = width / map.length;
        ShapeDrawer shapeDrawer = GameMain.shapeDrawer;
        shapeDrawer.filledRectangle(x,y,width,height,backgoundColor);
        int roundTimePercent=Math.round(timePercent*100f);
        float tempX=0;
        float tempY=0;
        for (int i = 0; i < map.length-1; i++) {
            if (i==roundTimePercent){
                tempX=i*xStep;
                tempY=map[i]*height;
            }
            shapeDrawer.line(
                    x+i*xStep,y+map[i]*height,
                    x+(i+1)*xStep,y+map[i+1]*height,1.5f);
        }
        float d1X = x + (x1 * width - dragSide / 2f);
        float d1Y = y + (y1 * height - dragSide / 2f);
        float d2X = x + (x2 * width - dragSide / 2f);
        float d2Y = y + (y2 * height - dragSide / 2f);
        shapeDrawer.filledRectangle(d1X, d1Y,dragSide,dragSide,Color.YELLOW);
        shapeDrawer.filledRectangle(d2X, d2Y,dragSide,dragSide,Color.YELLOW);
        shapeDrawer.line(x,y,d1X+dragSide/2f,d1Y+dragSide/2f,Color.YELLOW);
        shapeDrawer.line(x+width,y+height,d2X+dragSide/2f,d2Y+dragSide/2f,1,Color.YELLOW,Color.YELLOW);
        shapeDrawer.filledCircle(x+tempX,y+tempY,5, Color.RED);
    }

    public void regenMap(){
        dirty=false;
        map= MathUtil.genBezierPointMap(0,0,x1,y1,x2,y2,1,1,100);
    }

    public float[] getBezier(){
        return new float[]{x1,y1,x2,y2};
    }

    public void setBezier(Bezier2Interpolator interpolator){
        x1=interpolator.getX1();
        y1=interpolator.getY1();
        x2=interpolator.getX2();
        y2=interpolator.getY2();
        dirty=true;
    }

    public void setX1(float x1) {
        this.x1 = x1;
    }

    public void setY1(float y1) {
        this.y1 = y1;
    }

    public void setX2(float x2) {
        this.x2 = x2;
    }

    public void setY2(float y2) {
        this.y2 = y2;
    }

    public void setTimePercent(float timePercent) {
        this.timePercent = timePercent;
    }

    public interface BezierEditorWidgetUpdateInterface{
        void onBezierUpdated(float x1, float y1, float x2, float y2);
    }
}
